package com.yanqu.road.server.logic.activity.condition.peakstronghold;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.peakstronghold.PeakStrongHoldGuessValueArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class PeakStrongHoldGuessWinCondition extends BaseActivityCondition {

    public PeakStrongHoldGuessWinCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        PeakStrongHoldGuessValueArgs args = (PeakStrongHoldGuessValueArgs) object;
        int timeId = args.getTimeId();

        Map<Integer,Integer> rightCountUserCountMap = args.getRightCountUserCountMap();
        List<BigInteger> paramList = getActivityConditionInfo().getParamList();
        if(args.getConditionId() == getActivityConditionInfo().getConditionId()){
            if(paramList.get(2).compareTo(BigInteger.valueOf(timeId)) == 0){
                if(paramList.get(3).compareTo(args.getValue()) <= 0){
                    setValue(BigInteger.ONE);
                }
            }
        }

        //猜中人數變化了
        if(paramList.get(2).compareTo(BigInteger.valueOf(timeId)) == 0){
            int paramValue = 0;//没人猜对就0吧
            if(rightCountUserCountMap.containsKey(paramList.get(3).intValue())){
                paramValue = rightCountUserCountMap.get(paramList.get(3).intValue());
            }
            if(StringUtils.isNullOrEmpty(getParam()) || new BigInteger(getParam()).compareTo(BigInteger.valueOf(paramValue)) != 0){
                setParam(paramValue + "");
                player.getModule(ActivityModule.class).onActivityDataChanged(baseActivityData);
            }
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.PeakStrongHoldGuessWin.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.PeakStrongHoldGuessWin.getValue(), handle);
    }

    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(BigInteger.ZERO) > 0;
    }
}
